package com.aaa.moneymanage.controller.api;

import com.aaa.entity.*;
import com.aaa.moneymanage.feign.InvenoryFeign;
import com.aaa.moneymanage.services.CareHistoryServices;
import com.aaa.moneymanage.services.MoneyinServices;
import com.aaa.moneymanage.services.MoneyoutServices;
import com.aaa.moneymanage.utils.GetString;
import com.aaa.vo.Result;
// import com.sun.org.apache.regexp.internal.RE;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;

/**
 * @author ：XiaoxXuan
 * @date ：Created in 2023/3/23 19:20
 * @description：处方收费
 * @modified By：
 */
@RestController
@RequestMapping("/charge/api/prescription")
public class ApiCareHistoryController {

    @Resource
    private CareHistoryServices careHistoryServices;

    @Resource
    private MoneyinServices moneyinServices;

    @Resource
    private MoneyoutServices moneyoutServices;

    @Resource
    private GetString getString;

    @Resource
    private InvenoryFeign invenoryFeign;

    /** * @create by: XiaoXuan
     * @description: 处方收费获取页面
     * @create time: 2023/3/27 9:36
     * * @param regId 
     * @return com.aaa.vo.Result
     */
    @GetMapping("getOneByRegId")
    public Result getOne(String regId){
        return careHistoryServices.getOneByRegId(regId);
    }

    /** * @create by: XiaoXuan
     * @description: 处方退费获取页面
     * @create time: 2023/3/27 9:37
     * * @param regId 
     * @return com.aaa.vo.Result 
     */
    @GetMapping("backfeeByRegId")
    public Result backFee(String regId){
        return careHistoryServices.BackFeeByRegId(regId);
    }

    /** * @create by: XiaoXuan
     * @description: 处方发药获取页面
     * @create time: 2023/3/27 9:37
     * * @param regId 
     * @return com.aaa.vo.Result 
     */
    @GetMapping("dispensing")
    public Result dispensing(String regId){
        return careHistoryServices.dispensingByRegId(regId);
    }

    /** * @create by: XiaoXuan
     * @description: 现金收费
     * @create time: 2023/3/30 21:18
     * * @param regId
     * @param itemIds 
     * @return com.aaa.vo.Result 
     */
    @Transactional
    @PostMapping("/cashPay/{regId}/{allAmount}")
    public Result CashPay(@PathVariable String regId,@PathVariable String allAmount ,@RequestBody String[] itemIds ){

        //生成orderId
        String orderId = "ODC";
        Random random = new Random();
        for (int i = 0; i < 10; i++) {
            orderId += String.valueOf(random.nextInt(10));
        }

        String username = getString.getUsername();

        //修改状态
        for (String itemId : itemIds) {
            careHistoryServices.updateTheStatus(itemId,"1");
        }

        LocalDateTime now = LocalDateTime.now();
        BigDecimal bigDecimal = new BigDecimal(allAmount);

        CareHistory careHistory = careHistoryServices.getCareHistoryByRegId(regId);

        List<CareOrderItem> careOrderItems = careHistoryServices.getCareOrderItemByRegId(regId);

        HashMap<String, List<CareOrderItem>> stringListHashMap = new HashMap<>();
        for (String itemId : itemIds) {
            List<CareOrderItem> collect = careOrderItems.stream().filter(item -> item.getItemId().equals(itemId)).collect(Collectors.toList());
            stringListHashMap.put(itemId,collect);
        }

        OrderCharge orderCharge = new OrderCharge();
        orderCharge.setOrderId(orderId);
        orderCharge.setOrderAmount(bigDecimal);
        orderCharge.setChId(careHistory.getChId());
        orderCharge.setRegId(regId);
        orderCharge.setPatientName(careHistory.getPatientName());
        orderCharge.setOrderStatus("1");
        orderCharge.setPayPlatformId("114514");
        orderCharge.setPayTime(now);
        orderCharge.setPayType("0");
        orderCharge.setCreateTime(now);
        orderCharge.setCreateBy(username);

        moneyinServices.insertOrderCharge(orderCharge);


        for (String s : stringListHashMap.keySet()) {

            CareOrderItem careOrderItem = stringListHashMap.get(s).get(0);
            OrderChargeItem orderChargeItem = new OrderChargeItem();
            orderChargeItem.setItemId(s);
            orderChargeItem.setCoId(careOrderItem.getCoId());
            orderChargeItem.setItemName(careOrderItem.getItemName());
            orderChargeItem.setItemPrice(careOrderItem.getPrice());
            orderChargeItem.setItemNum(careOrderItem.getNum().intValue());
            orderChargeItem.setItemAmount(careOrderItem.getAmount());
            orderChargeItem.setOrderId(orderId);
            orderChargeItem.setItemType(careOrderItem.getItemType());
            orderChargeItem.setStatus(careOrderItem.getStatus());

            moneyinServices.insertOrderChargeItem(orderChargeItem);

        }

        return new Result(200,"蹦蹦炸弹");
    }

    /** * @create by: XiaoXuan
     * @description: 现金退费
     * @create time: 2023/3/30 21:18
     * * @param regId
     * @param itemIds 
     * @return com.aaa.vo.Result 
     */
    @Transactional
    @PostMapping("/cashBackfee/{regId}/{allAmount}")
    public Result CashBackfee(@PathVariable String regId,@PathVariable String allAmount ,@RequestBody String[] itemIds ){

        //生成orderId
        String backId= "ODB";
        Random random = new Random();
        for (int i = 0; i < 10; i++) {
            backId += String.valueOf(random.nextInt(10));
        }

        String username = getString.getUsername();

        //修改药方表状态
        for (String itemId : itemIds) {
            careHistoryServices.updateTheStatus(itemId,"2");
        }
        //修改收费查询表状态,顺便获取订单id
        String orderId = careHistoryServices.updateOrderChargeItemStatusByRegId(regId);

        LocalDateTime now = LocalDateTime.now();
        BigDecimal amount = new BigDecimal(allAmount);

        CareHistory careHistory = careHistoryServices.getCareHistoryByRegId(regId);

        List<CareOrderItem> careOrderItems = careHistoryServices.getCareOrderItemByRegId(regId);

        HashMap<String, List<CareOrderItem>> stringListHashMap = new HashMap<>();
        for (String itemId : itemIds) {
            List<CareOrderItem> collect = careOrderItems.stream().filter(item -> item.getItemId().equals(itemId)).collect(Collectors.toList());
            stringListHashMap.put(itemId,collect);
        }

        OrderBackfee orderBackfee = new OrderBackfee();
        orderBackfee.setBackId(backId);
        orderBackfee.setBackAmount(amount);
        orderBackfee.setChId(careHistory.getChId());
        orderBackfee.setRegId(regId);
        orderBackfee.setPatientName(careHistory.getPatientName());
        orderBackfee.setBackStatus("1");
        orderBackfee.setBackType("0");
        orderBackfee.setOrderId(orderId);
        orderBackfee.setBackPlatformId("414");
        orderBackfee.setBackTime(now);
        orderBackfee.setCreateTime(now);
        orderBackfee.setCreateBy(username);

        moneyoutServices.insertOrderBackfee(orderBackfee);

        for (String s : stringListHashMap.keySet()) {

            CareOrderItem careOrderItem = stringListHashMap.get(s).get(0);
            OrderBackfeeItem orderBackfeeItem = new OrderBackfeeItem();
            orderBackfeeItem.setItemId(s);
            orderBackfeeItem.setCoId(careOrderItem.getCoId());
            orderBackfeeItem.setItemName(careOrderItem.getItemName());
            orderBackfeeItem.setItemPrice(careOrderItem.getPrice());
            orderBackfeeItem.setItemNum(careOrderItem.getNum().intValue());
            orderBackfeeItem.setItemAmount(amount);
            orderBackfeeItem.setBackId(backId);
            orderBackfeeItem.setItemType(careOrderItem.getItemType());
            orderBackfeeItem.setStatus(careOrderItem.getStatus());

            moneyoutServices.insertOrderBackfeeItem(orderBackfeeItem);

        }

        return new Result(200,"炸鱼");
    }


    /** * @create by: XiaoXuan
     * @description: 发药
     * @create time: 2023/3/30 21:18
     * * @param regId
     * @param itemIds 
     * @return com.aaa.vo.Result 
     */
    @Transactional
    @PostMapping("/dispensing/{regId}")
    public Result dispensing(@PathVariable String regId , @RequestBody String[] itemIds ){

        List<CareOrderItem> careOrderItem = careHistoryServices.getCareOrderItemByRegId(regId);

        HashMap<String, List<CareOrderItem>> stringListHashMap = new HashMap<>();

        //修改药方表状态
        for (String itemId : itemIds) {
            careHistoryServices.updateTheStatus(itemId,"3");
            careHistoryServices.updateOrderChargeItemStatus(itemId,"3");
            List<CareOrderItem> collect = careOrderItem.stream().filter(item -> item.getItemId().equals(itemId)).collect(Collectors.toList());
            stringListHashMap.put(itemId,collect);
        }

        HashMap<String, BigDecimal> map = new HashMap<>();

        for (String s : stringListHashMap.keySet()) {
            String id = stringListHashMap.get(s).get(0).getItemRefId();
            BigDecimal num = stringListHashMap.get(s).get(0).getNum();
            map.put(id,num);
        }

        invenoryFeign.downInvenory(map);

        return new Result(200,"可莉害了");
    }


}